package com.xiaobao.coalproperty.module.user.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.BlankEntity;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

public class ForgetDealPasswordActivity extends BaseActivity {

    @BindView(R.id.forget_deal_phone)
    EditText edtPhone;
    @BindView(R.id.forget_deal_verification)
    EditText edtVerification;
    @BindView(R.id.forget_deal_pwd)
    EditText edtPwd;
    @BindView(R.id.forget_deal_pwd_again)
    EditText edtPwdAgain;

    @Override
    protected int getContentView() {
        return R.layout.activity_forget_deal_password;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("找回交易密码");
    }

    public void onForgetDealPwdVerificationClick(View view) {
        if (!isMobileNO(edtPhone.getText().toString())) {
            showToast("手机号码格式不正确");
            return;
        }
        sendCode();
    }

    public void onForgetDealPwdSubmitClick(View view) {
        if (!isMobileNO(edtPhone.getText().toString())) {
            showToast("手机号码格式不正确");
            return;
        }
        if (TextUtils.isEmpty(edtVerification.getText().toString())) {
            showToast("验证码不能为空");
            return;
        }
        if (TextUtils.isEmpty(edtPwd.getText().toString())) {
            showToast("密码不能为空");
            return;
        }
        if (!edtPwd.getText().toString().equals(edtPwdAgain.getText().toString())) {
            showToast("两次密码不一致");
            return;
        }
        if (edtPwd.getText().toString().length() < 6 || edtPwd.getText().toString().length() > 12) {
            showToast("密码长度在6-12之间");
            return;
        }
        if (isSpecial(edtPwd.getText().toString())) {
            showToast("密码不允许包含特殊字符");
            return;
        }
        submit();
    }

    private void submit() {
        OkHttpUtils.post().url(UrlConstant.URL_FORGET_TRANSACTION_PASSWORD)
                .params(HttpHelperUtils.getInstance().forgetTransactionPasswordParams(edtPhone.getText().toString(), edtVerification.getText().toString(), edtPwd.getText().toString()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    showToast(entity.getMsg());
                    if (entity.getCode() == 0) {
                        finish();
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    private void sendCode() {
        OkHttpUtils.post().url(UrlConstant.URL_SENDCODE)
                .params(HttpHelperUtils.getInstance().sendCodeParams(edtPhone.getText().toString(), "1"))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    if (entity.getCode() == 0) {
                        showToast("短信已发送，请注意查收");
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }

    public boolean isMobileNO(String mobileNums) {

        String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";// "[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        if (TextUtils.isEmpty(mobileNums))
            return false;
        else
            return mobileNums.matches(telRegex);
    }

    public boolean isSpecial(String mobileNums) {

        String telRegex = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        if (TextUtils.isEmpty(mobileNums))
            return false;
        else
            return mobileNums.matches(telRegex);
    }
}
